/**
 * 日期格式化
 * @param {String} date 年-月-日 时:分:秒
 * @returns String
 */
//都换算成毫秒
var second = 1000
var minute = 1000 * 60
var hour = minute * 60
var day = hour * 24
var week = day * 7
var month = day * 30
var year = month * 12
function dateFormat(date) {
    var nowDay = new Date()  //现在的时间
    var oldDay = new Date(date)  //自己要设定的时间
    var time = nowDay - oldDay
    if (time < 0) {
        return parseInt(-time / second) + '秒后';
    } else if (time / second <= 30) {
        return '刚刚';
    } else if (time / second < 60) {
        return parseInt(time / second) + '秒前';
    } else if (time / minute < 60) {
        return parseInt(time / minute) + '分钟前';
    } else if (time / hour < 24) {
        return parseInt(time / hour) + '小时前';
    } else if (time / day < 7) {
        return parseInt(time / day) + '天前';
    } else if (time / week < 4) {
        return parseInt(time / week) + '周前';
    } else if (time / month < 12) {
        return parseInt(time / month) + '月前';
    } else if (time / year > 1) {
        return parseInt(time / year) + '年前';
    }

}

/**
 * 字符串补零（注：传递来的数字长度不勾2位就左侧补充0
 * @param {String|Number} data  比如：日期数据4
 * @returns 过滤后的数据
 */
function zeroize(time) {
    return String(time).length === 1 ? '0' + time : time
}

/**
 * 快速获取当前时间
 * @param {String} str 举例：YmdHis  Y/m/d 
 * @returns String
 */
function format(str) {
    var d = new Date()
    str = str.replaceAll('Y', d.getFullYear())
    // var m = String(d.getMonth()+1).padStart(2, 0)
    // str = str.replaceAll('m', m)
    str = str.replaceAll('m', zeroize(d.getMonth() + 1))
    str = str.replaceAll('d', zeroize(d.getDate()))
    str = str.replaceAll('H', zeroize(d.getHours()))
    str = str.replaceAll('i', zeroize(d.getMinutes()))
    str = str.replaceAll('s', zeroize(d.getSeconds()))
    return str
}


/**
 * 随机颜色
 * @returns String
 */
function randomColor() {
    var colorArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
    var min = 0
    var max = colorArr.length - 1
    var first = colorArr[Math.floor(Math.random() * (max - min + 1) + min)]
    var two = colorArr[Math.floor(Math.random() * (max - min + 1) + min)]
    var three = colorArr[Math.floor(Math.random() * (max - min + 1) + min)]
    var four = colorArr[Math.floor(Math.random() * (max - min + 1) + min)]
    var five = colorArr[Math.floor(Math.random() * (max - min + 1) + min)]
    var six = colorArr[Math.floor(Math.random() * (max - min + 1) + min)]
    // console.log('#'+first+two+three+four+five+six)
    return '#' + first + two + three + four + five + six
}





//------------------------------------------------------------------------
/**
 * 判断浏览器类型
 * @param {} 
 * @returns {str}
*/
function getOs() {

    if (navigator.userAgent.indexOf('Firebox') !== -1) return '火狐浏览器'
    if (navigator.userAgent.indexOf('Chrome') !== -1) return '谷歌浏览器'
    if (navigator.userAgent.indexOf('Safari') !== -1) return '苹果浏览器'
    if (navigator.userAgent.indexOf('Opera') !== -1) return 'Opera浏览器'
    if (navigator.userAgent.indexOf('Trident') !== -1) return 'IE浏览器'

}

/**
 * 判断是PC还是移动端
 * @param {} 
 * @returns {str}
*/

function getPCMob() {

    data = navigator.userAgent
    if ((data.indexOf('Mac') !== -1 || data.indexOf('Windows') !== -1 || data.indexOf('Linux')) !== -1) {

        return 'PC端'
    }
    if ((data.indexOf('iPhone') !== -1 || data.indexOf('iPad') !== -1 || data.indexOf('Android') || data.indexOf('Mobile')) !== -1) 
    {return '移动端'

}
}



/**
 * //判断是否是微信内置浏览器 toLowerCase把字符串变小写
 * @returns 
 */
function isWeiXin() {
    var ua = navigator.userAgent.toLowerCase();   
   
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return '是微信内置浏览器';
    } else {
        return '我不是微信浏览器';
    }
}
//---------------------------------------------------
/**
 * 获取标签非行内样式
 * @param {Object} tagObj // 标签对象
 * @param {String} cssAttr // CSS属性值
 * @returns String
 */
 function getStyle(tagObj,cssAttr){
    if(window.getComputedStyle){
        return getComputedStyle(tagObj)[cssAttr]
    } else {
        return tagObj.currentStyle[cssAttr]
    }
}